package com.base.api.pubg.impl;

import com.base.api.pubg.IPubgDataTransactionService;
import com.base.dao.pubg.JsondataMapper;
import com.base.dao.pubg.LogitemunequipMapper;
import com.base.dao.pubg.LogplayerpositionMapper;
import com.base.vo.pubg.Jsondata;
import com.base.vo.pubg.Logitemunequip;
import com.base.vo.pubg.Logplayerposition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * PUBG数据事务服务
 *
 * @author: junw
 * @date: 2018/6/21 16:28
 */
@Service
public class PubgDataTransactionServiceImpl implements IPubgDataTransactionService {

    private LogitemunequipMapper logitemunequipMapper;

    private LogplayerpositionMapper logplayerpositionMapper;

    private JsondataMapper jsondataMapper;

    @Autowired
    public PubgDataTransactionServiceImpl(LogitemunequipMapper logitemunequipMapper, LogplayerpositionMapper logplayerpositionMapper, JsondataMapper jsondataMapper) {
        this.logitemunequipMapper = logitemunequipMapper;
        this.logplayerpositionMapper = logplayerpositionMapper;
        this.jsondataMapper = jsondataMapper;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 60, rollbackFor = Exception.class)
    public void insertData(List<Map<String, String>> list, Jsondata jsondata) {
        String commonMatchid = "";
        for (Map<String, String> map : list) {
            if ("LogItemUnequip".equals(map.get("_T"))) {
                commonMatchid = map.get("common_matchId");
                logitemunequipMapper.insert(getLogItemUnequipVO(map));
            } else if ("LogPlayerPosition".equals(map.get("_T"))) {
                if (logplayerpositionMapper.selectCount(map.get("common_matchId"), map.get("character_accountId"), map.get("_D")) < 1) {
                    logplayerpositionMapper.insertSelective(getLogplayerpositionVO(map));
                }
            }
        }
        jsondata.setMatchid(commonMatchid);
        jsondataMapper.insert(jsondata);
    }

    /**
     * 组装LogitemunequipVO
     *
     * @param map 集合数据
     * @return VO对象
     */
    public Logitemunequip getLogItemUnequipVO(Map<String, String> map) {
        Logitemunequip logItemUnequip = new Logitemunequip();
        logItemUnequip.setT(map.get("_T"));
        logItemUnequip.setD(map.get("_D"));
        logItemUnequip.setV(map.get("_V"));
        logItemUnequip.setCommonMatchid(map.get("common_matchId"));
        logItemUnequip.setCommonMapname(map.get("common_mapName"));
        logItemUnequip.setCommonIsgame(map.get("common_isGame"));
        logItemUnequip.setCharacterName(map.get("character_name"));
        logItemUnequip.setCharacterTeamid(map.get("character_teamId"));
        logItemUnequip.setCharacterHealth(map.get("character_health"));
        logItemUnequip.setCharacterName(map.get("character_name"));
        logItemUnequip.setCharacterLocationX(map.get("character_location_x"));
        logItemUnequip.setCharacterLocationY(map.get("character_location_y"));
        logItemUnequip.setCharacterLocationZ(map.get("character_location_z"));
        logItemUnequip.setCharacterRanking(map.get("character_ranking"));
        logItemUnequip.setCharacterAccountid(map.get("character_accountId"));
        logItemUnequip.setItemItemid(map.get("item_itemId"));
        logItemUnequip.setItemSubcategory(map.get("item_subCategory"));
        logItemUnequip.setItemAttacheditems(map.get("item_attachedItems"));
        logItemUnequip.setItemCategory(map.get("item_category"));
        logItemUnequip.setItemStackcount(map.get("item_stackCount"));
        return logItemUnequip;
    }

    /**
     * 组装LogplayerpositionVO
     *
     * @param map 集合数据
     * @return VO对象
     */
    public Logplayerposition getLogplayerpositionVO(Map<String, String> map) {
        Logplayerposition logplayerposition = new Logplayerposition();
        logplayerposition.setT(map.get("_T"));
        logplayerposition.setD(map.get("_D"));
        logplayerposition.setV(map.get("_V"));
        logplayerposition.setCommonMatchid(map.get("common_matchId"));
        logplayerposition.setCommonMapname(map.get("common_mapName"));
        logplayerposition.setCommonIsgame(map.get("common_isGame"));
        logplayerposition.setCharacterName(map.get("character_name"));
        logplayerposition.setCharacterTeamid(map.get("character_teamId"));
        logplayerposition.setCharacterHealth(map.get("character_health"));
        logplayerposition.setCharacterName(map.get("character_name"));
        logplayerposition.setCharacterLocationX(map.get("character_location_x"));
        logplayerposition.setCharacterLocationY(map.get("character_location_y"));
        logplayerposition.setCharacterLocationZ(map.get("character_location_z"));
        logplayerposition.setCharacterRanking(map.get("character_ranking"));
        logplayerposition.setCharacterAccountid(map.get("character_accountId"));
        logplayerposition.setNumaliveplayers(map.get("numAlivePlayers"));
        logplayerposition.setElapsedtime(map.get("elapsedTime"));
        return logplayerposition;
    }
}
